jQuery.fn.daterange = function(input, startDateInput, endDateInput) {
	var $datepicker = $(this);
	var $input = $('#' + input);
	
	$input.focus(function() {
		calendar.showCalendar($input, $datepicker, 0);
	});
	
	$(document).click(function(e){
		var $parents = $(e.target).parents().add(e.target);
    if (!$parents.filter('.ui-datepicker-group').length && !$parents.filter('#' + input).length ) {
    	$datepicker.hide();
    }
	});
	$datepicker.datepicker({
		firstDay : 1,
    numberOfMonths: 2,
    minDate : new Date(),
    maxDate : '+1Y -1d',
    dateFormat: 'dd/mm/yy',
    beforeShowDay: function(date) {
      var dateString = date.getTime();
      var text = ((dateString + 12 * 3600 * 1000) >= calendar.start && dateString <= calendar.end) ? dateString + ' highlight' : dateString;
        return [true, text, ''];
      },
      onSelect: function (dateText, inst) {
      	calendar.end = null;
        calendar.selectCount++;
      	calendar.currentDate = calendar.getSelectedDate(inst);
        calendar.current = calendar.currentDate.getTime();
        calendar.currentDate = dateText;
 
        if (calendar.selectCount < 2) {
        	
        	calendar.startDate = calendar.getSelectedDate(inst);
          calendar.start = calendar.startDate.getTime();
          calendar.startDate = dateText;
          calendar.endDate = null;
 
          $datepicker.oneTime(1, function() {
          	// подсветка
	      	$(".ui-datepicker-inline td a").live({
	              mouseenter: function() {
	              	if (calendar.selectCount) {
	                	var start = calendar.start;               	
	                  var end = calendar.getTimeClass(this);
	                  if (start > end) {
	                    end = calendar.start;
	                    start = calendar.getTimeClass(this);	
	                  } 
	                  $("td a").each(function (i) {
	                  	var t = calendar.getTimeClass(this);
	                    if (t >= start && t <= end) {
	                    	$(this).addClass('ui-state-active').removeClass('ui-state-highlight');
	                    }
	                  });
	              	}
	              },
	              mouseleave: function()  {
	              	$('td a').not('td a.' + calendar.start).removeClass('ui-state-active');
	              }
	        	});
	        });
      }
      else{
      	calendar.selectCount = 0;
      	calendar.endDate = calendar.getSelectedDate(inst);
        calendar.end = calendar.endDate.getTime();
        calendar.endDate = dateText;
        
        if (calendar.start == calendar.end) {
        	calendar.selectCount = 1;
        	calendar.endDate = null;
          calendar.end = null;       	
        }
        else {
	              
	        if (calendar.start > calendar.end) {
	        	calendar.endDate = calendar.startDate;
	        	calendar.startDate = calendar.currentDate;	
	          calendar.end = calendar.start;
	          calendar.start = calendar.current;	
	        }
	        if (calendar.end - calendar.start > 30 * 24 * 3600 * 1000) {
	        	App.error(arrLang['Диапазон дат должен быть меньше 30 дней']);
	        	$input.val('');
	        	$('#' + startDateInput).val('');
	        	$('#' + endDateInput).val('');
	        	calendar.endDate = null;
	        	calendar.startDate = null;	
	          calendar.end = null;
	          calendar.start = null;	
	          calendar.selectCount = 0;	
	        }
	        else {
	        	$input.val(calendar.getStartDate() + ' - ' + calendar.getEndDate());
	        	$('#' + startDateInput).val(calendar.startDate);
	        	$('#' + endDateInput).val(calendar.endDate);
	        	$datepicker.oneTime(1, function() {
	           	$datepicker.hide(); 
	            $('.ui-datepicker-inline td a').removeClass('ui-state-active');
	          });
	        	if (calendar.autoSearch) $input.closest('form').submit();
	        }          
	      }
      }
    }
	}).datepicker('setDate', new Date(calendar.start));
};

var calendar = {
	startDate: null, 
	endDate: null, 
  start: null, 
  end: null, 
	currentDate: new Date(), 
  current: null, 
	selectCount: 0,
	autoSearch: 0,
	getSelectedDate: function(inst){
		return new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
	},
	getStartDate: function() {
		var startDate = this.makeDate(this.startDate);
		return this.monthCase($.datepicker.formatDate('dd MM', startDate));
	},
	getEndDate: function() {
		var endDate = this.makeDate(this.endDate);
		return this.monthCase($.datepicker.formatDate('dd MM', endDate));
	},
	makeDate: function(date) {
		if (!date) return new Date(0);
	  var arrDate = date.split('/');
	  return new Date(arrDate[2], arrDate[1] - 1, arrDate[0]);			
	},
	monthCase: function(date) {
		if (lang != 'ru') return date;
		var lastSymbol = date.substring(date.length - 1);
		if (lastSymbol == 'ь' || lastSymbol == 'й') return date.substring(0, date.length - 1) + 'я';
		return date + 'а';			
	},
	getTimeClass: function(elem) {
		var classes = $(elem).parent().attr('class');
		if (classes == undefined) return null;
		var long = classes.match(/[0-9]+/);
		if (!long) return null;
		return parseInt(classes.match(/[0-9]+/)[0]); 		
	},
	showCalendar: function(elem, datepicker, autoSearch) {
		var $input = $(elem);
		var $datepicker = $(datepicker);
		var top = $input.offset().top + $input.height() + 12;
		if ((top - $(window).scrollTop() + 201) > $(window).height()) top = $(window).height() + $(window).scrollTop() - 211;
		var left = $input.offset().left;
		if (left > $(window).width() - 497) left = $(window).width() - 497;
		$datepicker.css({'left':left, 'top': top});
		$datepicker.datepicker();
		$datepicker.show();
		calendar.autoSearch = autoSearch;
		return !1;
	}
};